package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.RightsMapper;
import com.jt.pojo.Rights;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/*@Service
public class RightsServiceImpl  implements  RightsService{
    @Autowired
    private RightsMapper rightsMapper;

    @Override
    public List<Rights> getAll() {
        return rightsMapper.selectList(null);
    }




    *//*需求:查寻二级菜单信息
    * 步骤:
    *       1.查寻一级菜单信息    parent_id=0
    *       2.查寻二级菜单信息    parent_id=一级菜单ID
    *       3.将二级菜单封装到一级菜单当中
    *
    * */
/*
    @Override
    public List<Rights> getRightsList() {
        QueryWrapper  queryWrapper =new QueryWrapper();
        queryWrapper.eq("parent_id", 0);

        List<Rights> oneList =  rightsMapper.selectList(queryWrapper);
        //分析:1级和2级有各自的对应的关系,拿到当前一级查询自己的二级菜单
        for(Rights oneRights : oneList){
            //首先清空原有条件
            queryWrapper .clear();
            //然后根据一级id查寻二级信息.
            queryWrapper.eq("parent_id", oneRights.getId());
            List<Rights> twoList = rightsMapper.selectList(queryWrapper);
            //实现父子关系的封装
            oneRights.setChildren(twoList);

        }

        return oneList;
    }
}*/

@Service
public class RightsServiceImpl  implements  RightsService{
    @Autowired
    private RightsMapper rightsMapper;

    @Override
    public List<Rights> getAll() {
        return  rightsMapper.selectList(null);
    }

    @Override
    public List<Rights> getRightsList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",0);
        List<Rights> oneList = rightsMapper.selectList(queryWrapper);
        for(Rights oneRights : oneList){
            queryWrapper.clear();
            queryWrapper.eq("parent_id",oneRights.getId());
            List<Rights> twoList = rightsMapper.selectList(queryWrapper);
            oneRights.setChildren(twoList);
        }
        return oneList;
    }
}